package cn.tedu.boot022.controller;

import cn.tedu.boot022.entity.Product;
import cn.tedu.boot022.util.DBUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

@Controller
public class ProductController {
    @RequestMapping("/insert")
    @ResponseBody
    public String insert(Product product){
        System.out.println("product = " + product);
        try(Connection conn = DBUtil.getConnection()) {
            String sql = "insert into product values(null,?,?,?)";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, product.getTitle());
            ps.setDouble(2,product.getPrice());
            ps.setInt(3,product.getNum());
            ps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return "添加完成!<a href='/'>返回首页</a>";
    }

    @RequestMapping("/select")
    @ResponseBody
    public String select(){
        ArrayList<Product> list = new ArrayList<>();
        try ( Connection conn = DBUtil.getConnection()){
           String sql = "select id,title,price,num from product";
           PreparedStatement ps = conn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            while (rs.next()){
                int id = rs.getInt(1);
                String title = rs.getString(2);
                double price = rs.getDouble(3);
                int num = rs.getInt(4);
                list.add(new Product(id,title,price,num));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        String html = "<table border='2'>";
        html += "<caption>商品列表</caption>";
        html += "<tr><th>id</th><th>商品标题</th><th>商品价格</th><th>商品库存</th><th>操作</th>";
        for(Product p:list){
            html += "<tr>";
            html += "<td>"+p.getId()+"</td>";
            html += "<td>"+p.getTitle()+"</td>";
            html += "<td>"+p.getPrice()+"</td>";
            html += "<td>"+p.getNum()+"</td>";
            html += "<td><a href='/delete?id="+p.getId()+"'>删除</a></td>";/*?获取参数*/
            html += "</tr>";
        }
        html += "</table>";
        return html;

    }

    @RequestMapping("/delete")
    @ResponseBody
    public String delete(int id){
        System.out.println("id = " + id);
        try (Connection conn = DBUtil.getConnection()){
            String sql = "delete from product where id=?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setInt(1,id);
            ps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return "删除完成!<a href='/select'>返回列表页面</a>";
    }
    @RequestMapping("/update")
    @ResponseBody
    public String update(Product product){
        System.out.println("product = " + product);
        try( Connection conn  = DBUtil.getConnection()) {
          String sql = "update product set title=?,price=?,num=? where id=?";
          PreparedStatement ps = conn.prepareStatement(sql);
          ps.setString(1,product.getTitle());
          ps.setDouble(2,product.getPrice());
          ps.setInt(3,product.getNum());
          ps.setInt(4,product.getId());
          ps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return "修改完成!<a href='/select'>返回列表页面</a>";
    }
}
